package cn.liulin.java8.stream;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * cn.liulin.java8.stream$
 * 创建流
 * @author ll
 * @date 2021-10-14 15:18:32
 **/
public class CreateStream {
    public static void main(String[] args) {
//        createOne();
//        createTwo();
//        createThree();
//        createFour();
//        createFive();
//        createSix();
        createSeven();
    }

    private static void createSeven() {
        Stream.generate(Math::random)
                .limit(10)
                .forEach(System.out::println);
    }

    private static void createSix() {
        Stream.iterate(new int[] {0, 1}, t -> new int[] {t[1], t[0] + t[1]})
                .limit(20)
                .forEach(d -> System.out.println("[" + d[0] + ", " + d[1] + "]"));
    }

    private static void createFive() {
        Stream.iterate(0, n -> n + 2)
                .limit(10)
                .forEach(System.out::println);
    }

    private static void createFour() {
        try {
            Stream<String> lines = Files.lines(Paths.get("data.txt"), Charset.defaultCharset());
            long count = lines.flatMap(line -> Arrays.stream(line.split(" ")))
                    .distinct()
                    .count();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void createThree() {
        int[] nums = {1,2,3,4,5};
        IntStream stream = Arrays.stream(nums);
    }

    private static void createTwo() {
        Stream<String> empty = Stream.empty();
    }

    private static void createOne() {
        Stream<String> stream = Stream.of("java8", "in", "action");
        stream.map(String::toUpperCase).forEach(System.out::println);
    }
}
